package com.tek.ding.faceding.presenter.login;

import com.tek.ding.faceding.base.BasePresenter;
import com.tek.ding.faceding.bean.AppVersion;
import com.tek.ding.faceding.bean.BaseBean;
import com.tek.ding.faceding.bean.Staff;
import com.tek.ding.faceding.bean.Store;
import com.tek.ding.faceding.network.exception.ApiException;
import com.tek.ding.faceding.network.exception.BaseException;
import com.tek.ding.faceding.network.rx.RxErrorHandles;
import com.tek.ding.faceding.network.rx.RxHttpResponseCompat;
import com.tek.ding.faceding.network.rx.observable.ProgressDialogObservable;
import com.tek.ding.faceding.utils.loading.CircleDialog;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<LoginContract.ILoginModel, LoginContract.LoginView> {

    private BaseBean<AppVersion> appVersionBaseBean;

    public LoginPresenter(LoginContract.ILoginModel model, LoginContract.LoginView view) {
        super(model, view);
    }

    public void login(String storeUid, String username, String password) {
        model.login(storeUid, username, password)
                .compose(RxHttpResponseCompat.compatResult())
                .subscribe(new ProgressDialogObservable<BaseBean<Staff>>(context, view) {
                    //不显示提示框
                    @Override
                    protected boolean isShowDialog() {
                        return true;
                    }

                    @Override
                    public void onNext(@NonNull BaseBean<Staff> baseBean) {
                        if (baseBean != null) {
                            view.onLoginOk(baseBean);
                        }
                    }
                });
    }

    public void checkVersionAndStores(String storeUid, String username, String address, String lng, String lat) {
        CircleDialog.show(context);
        model.checkNewVersionV2(storeUid, address, lng, lat)
                .flatMap(baseBean -> {
                    if (baseBean == null)
                        return Observable.error(new ApiException(-1, "未知错误"));
                    if (baseBean.success()) {
                        if (null == baseBean.getData()) {
                            return model.getStaffStores(storeUid, username);
                        } else {
                            appVersionBaseBean = baseBean;
                            return Observable.error(new ApiException(-9, "update"));
                        }
                    } else {
                        return Observable.error(new ApiException(-1, baseBean.getMsg()));
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseBean -> {
                    CircleDialog.dismiss(context);
                    if (baseBean == null) {
                        view.showError("未知错误");
                    } else {
                        if (baseBean.success())
                            view.onGetStaffStores(baseBean);
                        else
                            view.showError(baseBean.getMsg());
                    }
                }, throwable -> {
                    CircleDialog.dismiss(context);
                    if (throwable instanceof ApiException) {
                        ApiException apiException = (ApiException) throwable;
                        // 服务器返回的错误
                        if (apiException.getStatus() == -9) {
                            view.onCheckNewVersionOk(appVersionBaseBean);
                        } else {
                            view.showError(apiException.getMessage());
                        }
                    } else {
                        // 未知/服务器错误
                        BaseException baseException = new RxErrorHandles(context).handleError(throwable);
                        if (baseException == null) {
                            view.showError("未知错误");
                        } else {
                            //显示信息
                            view.showError(baseException.getMessage());
                        }
                    }
                });
    }

    public void getStoreList(String storeUid, String username) {
        model.getStaffStores(storeUid, username)
                .compose(RxHttpResponseCompat.compatResult())
                .subscribe(new ProgressDialogObservable<BaseBean<List<Store>>>(context, view) {
                    //不显示提示框
                    @Override
                    protected boolean isShowDialog() {
                        return true;
                    }

                    @Override
                    public void onNext(@NonNull BaseBean<List<Store>> baseBean) {
                        if (baseBean != null) {
                            view.onGetStaffStores(baseBean);
                        }
                    }
                });
    }

    public void checkVersionAndLogin(String storeUid, String username, String password, String address, String lng, String lat) {
        CircleDialog.show(context);
        model.checkNewVersionV2(storeUid, address, lng, lat)
                .flatMap(baseBean -> {
                    if (baseBean == null)
                        return Observable.error(new ApiException(-1, "未知错误"));
                    if (baseBean.success()) {
                        if (null == baseBean.getData()) {
                            return model.login(storeUid, username, password);
                        } else {
                            appVersionBaseBean = baseBean;
                            return Observable.error(new ApiException(-9, "update"));
                        }
                    } else {
                        return Observable.error(new ApiException(-1, baseBean.getMsg()));
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(baseBean -> {
                    CircleDialog.dismiss(context);
                    if (baseBean == null) {
                        view.showError("未知错误");
                    } else {
                        if (baseBean.success())
                            view.onLoginOk(baseBean);
                        else
                            view.showError(baseBean.getMsg());
                    }
                }, throwable -> {
                    CircleDialog.dismiss(context);
                    if (throwable instanceof ApiException) {
                        ApiException apiException = (ApiException) throwable;
                        // 服务器返回的错误
                        if (apiException.getStatus() == -9) {
                            view.onCheckNewVersionOk(appVersionBaseBean);
                        } else {
                            view.showError(apiException.getMessage());
                        }
                    } else {
                        // 未知/服务器错误
                        BaseException baseException = new RxErrorHandles(context).handleError(throwable);
                        if (baseException == null) {
                            view.showError("未知错误");
                        } else {
                            //显示信息
                            view.showError(baseException.getMessage());
                        }
                    }
                });
    }

}

